#encoding: utf-8


'''
找出系列中出现次数最多的元素

'''

from collections import Counter
from functools import wraps

def select_sort(origin_items, comp=lambda x,y: x<y):
	''' 简单选择排序 '''
	items = origin_items[:]
	print(items)
	for i in range(len(items) - 1):
		min_index = i
		for j in range(i+1, len(items)):
			if comp(items[j], items[min_index]):
				min_index = j
		items[i], items[min_index] = items[min_index], items[i]
	return items

def main():
	# itmes = [1,321,3123,4534,435,1231,6456,222]
	# print(select_sort(itmes))

	prices = {
		'aapl' : 191.88,
		'goog' : 1186.2,
		'ibm' : 25,
		'fb' : 22
	}

	prices2 = { key : value for key, value in prices.items() if value >100}
	print(prices2)


	words = [
		'look' , 'info' , 'my', 'eyes', 'look', 'into', 'my', 'eyes',
    	'the', 'eyes', 'the', 'eyes', 'the', 'eyes', 'not', 'around',
    	'the', 'eyes', "don't", 'look', 'around', 'the', 'eyes',
    	'look', 'into', 'my', 'eyes', "you're", 'under'
	]

	counter = Counter(words)
	print(counter.most_common(3))

	''' 高阶函数 '''
	print(map(square, [1,2,3,4,5]))
	print(map(lambda x: x**3, [1,2,3,4,5]))

	''' 输出函数执行时间的装饰器 '''


	record_time(square(2))





def record_time(func):
	@wraps(func)
	def wrapper(*args, **kwargs):
		start = time()
		result = func(*args, **kwargs)
		print(time() - start + '秒')
		return result
	return wrapper


def square(x):
	return x**2



''' 用装饰器来实现单例模式 '''

def singleton(cls):
	instances = {}
	locker = Lock()
	@wraps(cls)
	def wrapper(*args, **kwargs):
		if cls not in instances:
			with locker:
				instances[cls] = cls(*args, **kwargs)
		return instances[cls]
	return wrapper


@singleton
class President(object):
	"""docstring for President"""
	def __init__(self, arg):
		super(President, self).__init__()
		self.arg = arg



if __name__ == '__main__':
	main()





